$(function () {
	var obj = {
		id : 0,							//商品id
		NewPrice: $('.newP'),			//商品新价格
		OldPrice: $('.oldP'),			//商品旧价格
		info_price: $('.info_price'),	//信息框价格
		title: $('.good_name'),			//商品名称
		img: $('.img'),					//详情图片
		thumbnail: $('.thumbnail'),		//信息框缩略图
		stock: $('.Stock'),				//库存
		info_Stock: $('.info_Stock')	//信息框库存
	}
	// 取出商品列表界面传过来的参数  
	function GetRequest() {        
	    var url = location.search;   
	    var theRequest = new Object();        
	    if (url.indexOf("?") != -1) {         
	        var str = url.substr(1);           
	        strs = str.split("&");         
	        for(var i = 0; i < strs.length; i++) {          
	            theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);         
	        }          
	    }         
	    return theRequest;        
	}
    obj.id = GetRequest().id;	//取出id存入变量对象
    console.log(obj.id);
	//进入商品详情界面首先根据商品的id加载数据
	$.ajax({  
	    url:'pro_list',
	    data:{
	    	id:obj.id,
	        platform:1
	    },  
	    type:'post',  
	    dataType:'json',  
	    success:function(res) { 
	    	console.log(res);
	    	var arr = res.piclist.split(",");
			// 轮播图
			var Swiper = new SwiperLunBo();
			Swiper.carousel(arr).swiper().changeLunBo(arr);		//changeLunBo方法只能在swiperLunBo方法之后调用
			localStorage.setItem('price',res.price);
			localStorage.setItem('title',res.title);
			localStorage.setItem('src',res.thumbnail);
			localStorage.setItem('lgId',res.id);
			obj.NewPrice.text(res.price);
			obj.info_price.text(res.price);
			obj.title.text(res.title);
			obj.stock.text(res.stock);
			obj.info_Stock.text(res.stock);
			obj.thumbnail[0].src = res.thumbnail;
			var result = HTMLDecode(res.introduce);
			var box = $('.detailImg_box');
			var div = $('<div class="imgBefor"></div>');
			box.append(div.append(result));
	    },  
	    error : function(err) {    
	        console.log(err);
	    }  
	});//ajax结束
	
	//html标签反转译
	function HTMLDecode(text) {
        var temp = document.createElement("div");
        temp.innerHTML = text;
        var output = temp.innerText || temp.textContent;
        temp = null;
        return output;
    }
    
    //商品数量加减
    var total;																	//用于存储商品的总价钱
    var price = $(".price").text();												//商品的单价
    var num = $(".buy_num .num");												//选择商品数量
    //数量加按钮  
    $(".buy_num .add").click(function(){  
        num.val(parseInt(num.val()) + 1);  
        $(".buy_num .reduce").removeAttr("disabled");           //当按加1时，设置$(".buy_num .reduce")为可点击状态
    });
    //数量减按钮  
    $(".buy_num .reduce").click(function(){  
        if (parseInt(num.val()) > 1) {                 		 	//判断数量值大于1时才可以减少  
        	num.val(parseInt(num.val()) - 1);  
        }else{  
        	$(".buy_num .reduce").attr("disabled",true);  		//当num为1时，设置$(".buy_num .reduce")为不可点击状态
        	$(".errAlert").text("商品数量至少为一件").addClass("AlertShow");
        	setTimeout(function(){
	        	$(".errAlert").removeClass("AlertShow");
	        },3000);  
       	}
    });

    //加入购物车
    $(".detail_foot .shopingCar").bind("click",function(){
    	$('html,body').addClass('ovfHiden');
		$('.mask,.detail_footThree,.good_Info').bind("touchmove",function(e){
			e.preventDefault();
			e.stopPropagation();
		});
		$('.detail_foot').hide();
		$('.mask').show();	
		$('.good_Info').show();	
		$('.addCar').show().unbind('click').click(function(){
			var Number = $(".buy_num .num").val();
			console.log('id：' + obj.id,'数量：' + Number);
			$.ajax({  
			    url:'http://a.91jfk.com/Product/Zero/cart', 
			    type:'post',  
			    dataType:'json',
			    data:{  
		            id:obj.id,
		            num:Number
			    },  
			    success:function(res) { 
			    	console.log(res);
			    	if (res.code == 1) {
			    		$(".errAlert").text(res.msg).addClass("AlertShow");
			        	setTimeout(function(){
				        	$(".errAlert").removeClass("AlertShow");
				        },2000);
				        $('.mask').hide();
				        $('.good_Info').hide();
				        $('.addCar').hide();
				        $('.detail_foot').css('display','block');
				        setTimeout(function(){
				        	window.location.reload();
				        },3000);
			    	}else{
			    		$(".errAlert").text(res.msg).addClass("AlertShow");
			        	setTimeout(function(){
				        	$(".errAlert").removeClass("AlertShow");
				        },2000);
			    	}
			    },  
			    error : function(err) {    
			        console.log(err);
			    }  
			});//ajax结束
		});
    });//点击事件结束
    
    //立即购买
    $(".detail_foot .farstBuy").bind("click",function(){
    	$('html,body').addClass('ovfHiden');
		$('.mask,.payment_foot').bind("touchmove",function(e){
			e.preventDefault();
			e.stopPropagation();
		});
		$('.detail_foot').hide();
		$('.mask').show();
		$('.good_Info').show();
		$('.quickBuy').show().unbind('click').click(function(){
			console.log('立即购买...');
			var goodNum = $(".buy_num .num").val();
			localStorage.setItem('num',goodNum);
			window.location.href = 'confirmOrder.html';
		});
    });//点击事件结束
    
    //关闭商品信息
    $(".good_Info .close").click(function(){
		$('html,body').removeClass('ovfHiden');
    	$('.detail_foot').css('display','block');
        $(".mask").css("display","none");
        $('.addCar').hide();
		$('.quickBuy').hide();
        $('.good_Info').hide();
    });
    
    //点击首页按钮跳转到首页
	$('.foot_li_Lt').bind('click',function(){
		console.log('跳转到首页...')
		window.location.href = '/Product/Shopindex/index.html';
	});
	
	//点击购物车小图标跳转到购物车页面
	$('.shop_car').bind('click',function(){
		console.log('跳转到购物车...')
		window.location.href = 'gouwuche.html';
	});
	
	//返回按钮
	$('.goBack').bind('click',function(){
		window.location.href = document.referrer;	//返回上一页并刷新
	});
});